package cn.lhn.ridersclub.controller;

import cn.lhn.ridersclub.anno.TokenVerify;
import cn.lhn.ridersclub.dto.AccountCardDto;
import cn.lhn.ridersclub.dto.AccountDto;
import cn.lhn.ridersclub.dto.Result;
import cn.lhn.ridersclub.entity.Account;
import cn.lhn.ridersclub.entity.AccountFollow;
import cn.lhn.ridersclub.entity.Login;
import cn.lhn.ridersclub.service.AccountFollowService;
import cn.lhn.ridersclub.service.AccountService;
import cn.lhn.ridersclub.service.BlacklistService;
import cn.lhn.ridersclub.service.LoginService;
import org.hibernate.validator.constraints.Length;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/account")
public class AccountController {

    private final AccountService accountService;

    private final LoginService loginService;

    private final AccountFollowService accountFollowService;

    private final BlacklistService blacklistService;

    public AccountController(AccountService accountService, LoginService loginService, AccountFollowService accountFollowService, BlacklistService blacklistService) {
        this.accountService = accountService;
        this.loginService = loginService;
        this.accountFollowService = accountFollowService;
        this.blacklistService = blacklistService;
    }

    @PostMapping("/register")
    public Result<AccountDto> register(String email, String code, String nickname, String gender, String password) {
        AccountDto account = accountService.registerAccount(email, code, gender, nickname);
        loginService.registerLogin(account.getId(), email, password);
        return Result.success("注册成功", account);
    }

    @PostMapping("/close")
    public Result<AccountDto> close(String email, String code) {
        AccountDto account = accountService.closeAccount(email, code);
        loginService.closeLogin(account.getId());
        return Result.success("注销成功", account);
    }

    @GetMapping("/get")
    public Result<AccountDto> getAccount(long id) {
        AccountDto account = accountService.getAccount(id);
        return Result.success("查询成功", account);
    }

    @GetMapping("/getIdByEmail")
    public Result<Long> getIdByEmail(String email) {
        Login login = loginService.getLoginByEmail(email);
        if (login == null) {
            throw new RuntimeException("账号不存在");
        }

        return Result.success("查询成功", login.getId());
    }

    @GetMapping("/nickname")
    public Result<AccountDto> getAccounts(@Length(min = 2, max = 10) String nickname) {
        AccountDto account = accountService.getAccount(nickname);
        return Result.success("查询成功", account);
    }

    @TokenVerify
    @PostMapping("/update")
    public Result<?> updateAccount(Account account) {
        accountService.updateAccount(account);
        FlushController.accountMap.put(account.getId(), true);
        return Result.success().message("更新成功");
    }

    @GetMapping("/getFollows")
    public Result<List<AccountCardDto>> getFollows(Long accountId, Long startId) {
        List<AccountCardDto> follows = accountFollowService.getFollows(accountId, startId);
        return Result.success("查询成功", follows);
    }

    @GetMapping("/getFans")
    public Result<List<AccountCardDto>> getFans(Long accountId, Long startId) {
        List<AccountCardDto> follows = accountFollowService.getFans(accountId, startId);
        return Result.success("查询成功", follows);
    }

    @GetMapping("/getFollowCount")
    public Result<Long> getFollowCount(Long accountId) {
        Long count = accountFollowService.getFollowCount(accountId);
        return Result.success("查询成功", count);
    }

    @GetMapping("/getFansCount")
    public Result<Long> getFansCount(Long accountId) {
        Long count = accountFollowService.getFansCount(accountId);
        return Result.success("查询成功", count);
    }

    @PostMapping("/addFollow")
    @TokenVerify
    public Result<?> addFollow(Long accountId, HttpServletRequest request) {
        String fansId = request.getHeader("arg0");
        accountFollowService.addFollow(accountId, Long.parseLong(fansId));
        return Result.success().message("添加成功");
    }

    @PostMapping("/deleteFollow")
    @TokenVerify
    public Result<?> deleteFollow(Long accountId, HttpServletRequest request) {
        String fansId = request.getHeader("arg0");
        accountFollowService.deleteFollow(accountId, Long.parseLong(fansId));
        return Result.success().message("删除成功");
    }

    @GetMapping("/isFollow")
    @TokenVerify
    public Result<Boolean> isFollow(Long id, HttpServletRequest request) {
        String accountId = request.getHeader("arg0");
        boolean follow = accountFollowService.isFollow(id, Long.parseLong(accountId));
        return Result.success("查询成功", follow);
    }

    @PostMapping("/addBlack")
    @TokenVerify
    public Result<?> addBlack(Long targetId, HttpServletRequest request) {
        Long sourceId = Long.parseLong(request.getHeader("arg0"));
        blacklistService.add(sourceId, targetId);
        return Result.success().message("拉黑成功");
    }

    @PostMapping("/removeBlack")
    @TokenVerify
    public Result<?> removeBlack(Long targetId, HttpServletRequest request) {
        Long sourceId = Long.parseLong(request.getHeader("arg0"));
        blacklistService.delete(sourceId, targetId);
        return Result.success().message("移出成功");
    }

    @GetMapping("/isBlack")
    @TokenVerify
    public Result<Boolean> isBlack(Long targetId, HttpServletRequest request) {
        Long sourceId = Long.parseLong(request.getHeader("arg0"));
        boolean isBlack = blacklistService.isBlack(sourceId, targetId);
        return Result.success(isBlack);
    }

    @GetMapping("/listBlack")
    @TokenVerify
    public Result<List<Account>> listBlack(HttpServletRequest request) {
        Long sourceId = Long.parseLong(request.getHeader("arg0"));
        List<Account> list = blacklistService.listBlack(sourceId);
        return Result.success(list);
    }
}
